What is vscode-json-languageservice?
The vscode-json-languageservice npm package provides a set of services for working with JSON documents in a way that is compatible with the Visual Studio Code editor. It offers features such as validation, completion, hover information, and more, making it easier to build tools and extensions that work with JSON data.
What are vscode-json-languageservice's main functionalities?
Validation
This feature allows you to validate JSON documents. The code sample demonstrates how to use the language service to validate a JSON document and print out any diagnostics (errors or warnings) found.
const { getLanguageService } = require('vscode-json-languageservice');
const languageService = getLanguageService({});
const document = { uri: 'file://test.json', languageId: 'json', version: 1, getText: () => '{ "key": "value" }' };
const jsonDocument = languageService.parseJSONDocument(document);
languageService.doValidation(document, jsonDocument).then(diagnostics => {
console.log(diagnostics);
});
Completion
This feature provides auto-completion suggestions for JSON documents. The code sample shows how to get completion suggestions at a specific position in a JSON document.
const { getLanguageService } = require('vscode-json-languageservice');
const languageService = getLanguageService({});
const document = { uri: 'file://test.json', languageId: 'json', version: 1, getText: () => '{ "key": "value" }' };
const jsonDocument = languageService.parseJSONDocument(document);
languageService.doComplete(document, { line: 0, character: 1 }, jsonDocument).then(completionList => {
console.log(completionList);
});
Hover Information
This feature provides hover information for JSON documents. The code sample demonstrates how to get hover information at a specific position in a JSON document.
const { getLanguageService } = require('vscode-json-languageservice');
const languageService = getLanguageService({});
const document = { uri: 'file://test.json', languageId: 'json', version: 1, getText: () => '{ "key": "value" }' };
const jsonDocument = languageService.parseJSONDocument(document);
languageService.doHover(document, { line: 0, character: 1 }, jsonDocument).then(hover => {
console.log(hover);
});
Other packages similar to vscode-json-languageservice
jsonlint
jsonlint is a JSON parser and validator with a CLI. It is primarily used for validating JSON data and providing error messages. Unlike vscode-json-languageservice, it does not offer features like completion or hover information.
ajv
ajv is a JSON schema validator that supports JSON Schema draft-07 and other standards. It is highly performant and can be used for validating JSON data against schemas. While it focuses on validation, it does not provide language service features like completion or hover information.
json-schema-to-typescript
json-schema-to-typescript converts JSON schemas to TypeScript interfaces. It is useful for generating TypeScript types from JSON schemas but does not offer the comprehensive language services provided by vscode-json-languageservice.
vscode-json-languageservice
JSON language service extracted from VSCode to be reused, e.g in the Monaco editor.
Why?
The vscode-json-languageservice contains the language smarts behind the JSON editing experience of Visual Studio Code
and the Monaco editor.
- doValidation analyses an input string and returns syntax and lint errors.
- doComplete provides completion proposals for a given location.
- doResolve resolves a completion proposals.
- doHover provides a hover text for a given location.
- findDocumentSymbols provides all symbols in the given document.
- findDocumentColors provides all color symbols in the given document.
- getColorPresentations returns available color formats for a color symbol.
- format formats the code at the given range.
- getFoldingRanges gets folding ranges for the given document.
- getSelectionRanges gets selection ranges for a given location.
- getMatchingSchemas matches a document against its schema and returns all AST nodes along with the matching sub schemas.
- parseJSONDocument creates a JSON document from source code.
- newJSONDocument creates a JSON document from an AST.
For the complete API see jsonLanguageService.ts and jsonLanguageTypes.ts
Installation
npm install --save vscode-json-languageservice
Sample usage
See sample.ts for an example on how to use the JSON language service.
To run the sample use yarn sample
Development
git clone https://github.com/microsoft/vscode-json-languageservice
cd vscode-json-languageservice
yarn
Use yarn test
to compile and run tests
How can I run and debug the service?
- open the folder in VSCode.
- set breakpoints, e.g. in
jsonCompletion.ts
- run the Unit tests from the run viewlet and wait until a breakpoint is hit:
How can I run and debug the service inside an instance of VSCode?
- run VSCode out of sources setup as described here: https://github.com/Microsoft/vscode/wiki/How-to-Contribute
- use
yarn link vscode-json-languageservice
in vscode/extensions/json-language-features/server
to run VSCode with the latest changes from vscode-json-languageservice
- run VSCode out of source (
vscode/scripts/code.sh|bat
) and open a .json
file - in VSCode window that is open on the
vscode-json-languageservice
sources, run command Debug: Attach to Node process
and pick the code-oss
process with the json-language-features
path
- set breakpoints, e.g. in
jsonCompletion.ts
- in the instance run from sources, invoke code completion in the
.json
file